package com.yiboshi.rehrs.util;



public class RechargeCardUtil {

    static  int len = 9; //随机码
    static  int saltMark = 5; //校验位
    static  public String salt = "ac!@@@-123+XXX";
    static int num = 0; //天数占位
    static int length = len+saltMark+num;
    //len位随机+2day+3位md5(len随机+盐);
    public static String generate(){
        String randomCodeString = AperStringUtils.getRandomCodeString(len).toUpperCase();
        String dayNumber = "";
        /*vipDay = AperStringUtils.randomNum(100);
        if(vipDay<10){
            dayNumber = "0";
        }*/

        //dayNumber = dayNumber+vipDay;
        //randomCodeString = randomCodeString+dayNumber;
        String encode = MD5.encode(randomCodeString+salt).substring(2,saltMark+2).toUpperCase();
        String code = randomCodeString+encode;
        return code;
    }

    public static boolean checkRechargeCode(String code){
        if(StrUtils.isBlank(code)||code.length()!=length){
            return false;
        }
        String randomCodeString = code.substring(0,len+num).toUpperCase();
        String encode = MD5.encode(randomCodeString+salt).substring(2,saltMark+2).toUpperCase();
        String md5 = code.substring(len+num);
        return encode.equalsIgnoreCase(md5);
    }

    /*public static int checkAndRechargeDay(String code){
        if(StrUtils.isBlank(code)||code.length()!=length){
            return -1;
        }
        String randomCodeString = code.substring(0,len+num).toUpperCase();
        String encode = MD5.encode(randomCodeString+salt).substring(2,saltMark+2).toUpperCase();
        String md5 = code.substring(len+num);
        if(encode.equalsIgnoreCase(md5)){
            String substring = code.substring(len, len + num);
            if(AperStringUtils.isNumber(substring)){
                return Integer.valueOf(substring);
            }
        }
        return -1;
    }*/

   /* public static void main(String[] args) {
        for(int i=0;i<10;i++){
            System.out.println(generate());
        }
        String kami = "H3VJ8UANK5DB92";
        boolean b = checkRechargeCode(kami);
        System.out.println(b);
    }*/

    //邀请码8位随机，1位校验位
    public static String inviteCode(){
    	String randomCodeString = AperStringUtils.getRandomCodeString(8).toUpperCase();
        String substring = MD5.encode(randomCodeString + salt).substring(1, 2).toUpperCase();
        return randomCodeString + substring;
    }


    public static boolean checkInviteCode(String inviteCode){
        if(inviteCode==null||inviteCode.length()!=9){
            return false;
        }
        String randomCodeString =inviteCode.substring(0,8).toUpperCase();
        String substring = MD5.encode(randomCodeString + salt).substring(1, 2);
        return (randomCodeString + substring).equalsIgnoreCase(inviteCode);
    }


    public static void main(String[] args) {
        int a = 0;
      while (a++<800){
          String randomCodeString = AperStringUtils.getRandomCodeString(11).toUpperCase();
          System.out.println(randomCodeString);
      }



    }
}

